import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// 页面共用标题栏
class HzAppBar extends StatelessWidget implements PreferredSizeWidget {
  /// 前置控件展示状态
  final bool showLeading;

  /// 后置控件列表展示状态
  final bool showActions;

  /// 标题文本内容
  final String? text;

  final bool? centerTitle;

  /// 标题颜色
  final Color? titleColor;

  /// 标题大小
  final double? titleSize;

  /// 前置控件
  final Widget? leading;

  /// 后置控件列表
  final List<Widget>? actions;

  /// 标题控件
  final Widget? title;

  /// 标题控件高度
  final double? height;

  /// 标题栏背景色
  final Color? backgroundColor;

  /// 前置控件点击回调
  final VoidCallback? onLeadingTap;

  /// 背景Widget
  final Widget? flexibleSpace;

  /// 状态栏是否是深色，默认是
  final bool statusBarDark;

  const HzAppBar({
    super.key,
    this.text,
    this.centerTitle,
    this.titleColor,
    this.titleSize,
    this.leading,
    this.actions,
    this.height,
    this.title,
    this.showLeading = true,
    this.showActions = true,
    this.onLeadingTap,
    this.backgroundColor,
    this.statusBarDark = true,
    this.flexibleSpace,
  });

  /// 构建前置控件
  Widget _buildLeading(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        if (onLeadingTap != null) {
          onLeadingTap?.call();
        } else {
          Navigator.pop(context);
        }
      },
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        alignment: Alignment.center,
        child: Image.asset(
          'images/left.png',
          color: statusBarDark ? null : Colors.white,
          width: 16,
          height: 24,
        ),
      ),
    );
  }

  /// 构建标题内容控件
  Widget _buildTitle() {
    return Text(
      text ?? '',
      style: TextStyle(
        color: titleColor ??
            (statusBarDark ? const Color(0xff0F1724) : Colors.white),
        fontSize: titleSize ?? 18,
        fontWeight: FontWeight.w500,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: title ?? _buildTitle(),
      leading: leading ?? (showLeading ? _buildLeading(context) : null),
      titleSpacing: 0,
      actions: actions,
      centerTitle: centerTitle ?? true,
      toolbarHeight: height ?? 46.0,
      backgroundColor:
          backgroundColor ?? AppBarTheme.of(context).backgroundColor,
      flexibleSpace: flexibleSpace,
      scrolledUnderElevation: 0,
      automaticallyImplyLeading: false,
      systemOverlayStyle: SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarBrightness: statusBarDark ? Brightness.light : Brightness.dark,
        statusBarIconBrightness:
            statusBarDark ? Brightness.dark : Brightness.light,
      ),
      // systemOverlayStyle: statusBarDark ? SystemUiOverlayStyle.dark : SystemUiOverlayStyle.light,
    );
  }

  @override
  Size get preferredSize => _TitleBarSize(height, 0);
}

class _TitleBarSize extends Size {
  final double? toolbarHeight;
  final double? bottomHeight;

  _TitleBarSize(this.toolbarHeight, this.bottomHeight)
      : super.fromHeight((toolbarHeight ?? 46.0) + (bottomHeight ?? 0));
}

abstract class HzAppBarPopBehavior {
  void onPop() {
    throw Exception('not implement');
  }
}
